<template>
  <div class="app-container" style="padding:0">

    <div class="topLine">
      <!-- 搜索 -->
      <el-form ref="queryForm" :model="listQuery" :inline="true" label-width="68px">
        <el-form-item label="任务名称" prop="name">
          <el-input
            v-model="listQuery.name"
            placeholder="请输入任务名称"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="任务类型" >
          <el-select v-model="listQuery.type" placeholder="请选择类型" style="width:100%">
            <el-option
              v-for="item in taskTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮 -->
      <div>
        <!-- <el-button type="primary"  icon="el-icon-plus"  size="mini" >开始任务 </el-button> -->
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除 </el-button>
      </div>

    </div>

    <el-table v-loading="loading" :data="list" empty-text="暂无数据" @selection-change="handleSelectionChange">i
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" width="50" />
      <el-table-column label="任务名称" align="center" prop="name" />
      <!-- <el-table-column label="任务描述" align="center" prop="description"  :show-overflow-tooltip="true" /> -->
      <!-- <el-table-column label="任务类型" align="center"  >
        <template slot-scope="scope">
          <div v-if="scope.row.type==1">ST教学分析</div>
          <div v-if="scope.row.type==2">会议督察</div>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="组织" align="center" prop="department_id" />
      <el-table-column label="采样时间" align="center"  >
        <template slot-scope="scope">
          <div v-if="scope.row.sampling_interval==5">5秒</div>
          <div v-if="scope.row.sampling_interval==15">15秒</div>
          <div v-if="scope.row.sampling_interval==30">30秒</div>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="学生画面" align="center" prop="videoStream1" />
      <el-table-column label="老师画面" align="center" prop="videoStream2" /> -->
      <el-table-column label="开始时间" align="center" prop="start_time" />
      <el-table-column label="结束时间" align="center" prop="end_time" />
      <el-table-column label="任务状态" align="center" prop="status ">
        <template slot-scope="scope">
          <div v-if="scope.row.status==0">正在进行</div>
          <div v-if="scope.row.status==1">已结束</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="checkData(scope.row)"
          >查看数据
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getTaskList"
    />

  </div>
</template>

<script>

// 接口
import { getAllTask, deleteHistory } from '@/api/main'
import { listDept } from '@/api/system/dept'

import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'MeetingList',

  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 字典表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      listQuery: {
        page: 1,
        limit: 10,
        name: undefined,
        type: 2
      },
      taskTypes: [
        { value: 1, label: 'ST教学分析' },
        { value: 2, label: '会议督察' }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        url: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '模板名称不能为空', trigger: 'blur' }
        ]
      },
      interval: null,
      // 组织树选项
      deptOptions: undefined,
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  watch: {
    // 根据名称筛选组织树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    // 获取历史任务
    this.getTaskList()
    // /////////
    this.getTreeselect()
  },
  methods: {

    /** 查询组织下拉树结构 */
    getTreeselect() {
      listDept().then(response => {
        this.deptOptions = response.data
      })
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    // 获取历史任务
    getTaskList() {
      this.loading = true
      getAllTask(this.listQuery).then(response => {
        this.list = response.data.task_data
        this.list.forEach(item => {
          if (item.end_time) {
            item.end_time = item.end_time.replace('T', ' ')
          }
          if (item.start_time) {
            item.start_time = item.start_time.replace('T', ' ')
          }
        })
        this.total = response.data.total
        this.loading = false
      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        code: undefined,
        description: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.listQuery.page = 1
      this.getTaskList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.listQuery = {
        page: 1,
        limit: 10,
        name: undefined,
        type: undefined
      }
      this.handleQuery()
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 查看任务数据 */
    checkData(row) {
      const id = row.id
      // 跳转
      this.$router.push({ path: '/meetingAnalysis', query: { id: id }})
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认此纪录?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return deleteHistory(ids)
      }).then(() => {
        this.getTaskList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
  }
}
</script>

<style>
.topLine{
  padding:20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #fff;
}
.el-table .el-table__header-wrapper th{
  background: #e1ecfa;
}
.topLine .el-form-item{
  margin-bottom: 0;
}
</style>
